package com.first.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.constant.MessageConstant;
import com.first.common.context.BaseContext;
import com.first.common.exception.WorkAddException;
import com.first.common.result.PageResult;
import com.first.pojo.dto.PlaceWorkUserDTO;
import com.first.pojo.dto.PlaceWorkUserPageQueryDTO;
import com.first.pojo.entity.*;
import com.first.server.mapper.*;
import com.first.server.service.PlaceWorkUserService;
import com.first.server.service.WorkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.first.common.utils.StringToListUtil.StringToList;

@Service
@Slf4j
public class PlaceWorkUserServiceImpl implements PlaceWorkUserService {

    @Autowired
    PlaceMapper placeMapper;
    @Autowired
    WorkMapper workMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    private UserWorkMapper userWorkMapper;
    @Autowired
    PlaceWorkUserMapper placeWorkUserMapper;
    @Autowired
    UserPlaceMapper userPlaceMapper;

    @Override
    public PageResult pageQuery(PlaceWorkUserPageQueryDTO placeWorkUserPageQuery) {
        PageHelper.startPage(placeWorkUserPageQuery.getPage(), placeWorkUserPageQuery.getPageSize());
        Page<PlaceWorkUser> page = placeWorkUserMapper.pageQuery(placeWorkUserPageQuery);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    public void addWorkToPlace(PlaceWorkUserDTO placeWorkUserDTO) {
        Work work=workMapper.selectById(placeWorkUserDTO.getWorkId());
        User user = userMapper.selectById(placeWorkUserDTO.getUserId());
        Place place=placeMapper.selectById(placeWorkUserDTO.getPlaceId());

        UserWork userWork =userWorkMapper.selectOne(
                new QueryWrapper<UserWork>()
                        .lambda()
                        .eq(UserWork::getWorkId,work.getId())
                        .eq(UserWork::getAuthorId,user.getId())
        );


        if(userWork!=null){
            //不是作者
            throw new WorkAddException(MessageConstant.USER_NOTEXITS_WORK);
        }

        //该基地不是该老师所属基地
        QueryWrapper<UserPlace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UserPlace::getUserId,user.getId());
        List<UserPlace> userPlaceList = userPlaceMapper.selectList(queryWrapper);
        List<Integer> placeIdList=userPlaceList.stream().map(UserPlace::getPlaceId).collect(Collectors.toList());
        if(!placeIdList.contains(place.getId())){
            throw new WorkAddException(MessageConstant.USER_NOTEXITS_PLACE);
        }

        //查询出所有以作者身份成果对应的基地
        QueryWrapper<PlaceWorkUser> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .select(PlaceWorkUser::getPlaceId)
                .eq(PlaceWorkUser::getPlaceId,placeWorkUserDTO.getWorkId())
                .eq(PlaceWorkUser::getUserId,placeWorkUserDTO.getUserId());
        List<PlaceWorkUser> pwuList = placeWorkUserMapper.selectList(wrapper);

        //该成果已被该老师加入基地
        if(pwuList.size()!=0) {
            throw new WorkAddException(MessageConstant.WORK_CANNOTEXITS_PLACE);
        }

        //查询出所有以作者身份成果对应的基地
        QueryWrapper<PlaceWorkUser> wrapper1 = new QueryWrapper<>();
        wrapper.lambda()
                .eq(PlaceWorkUser::getWorkId,placeWorkUserDTO.getWorkId())
                .eq(PlaceWorkUser::getPlaceId,placeWorkUserDTO.getPlaceId());
        List<PlaceWorkUser> pwuList2 = placeWorkUserMapper.selectList(wrapper1);

        //该基地已存在该成果（或被其他成员加入）
        if(pwuList2.size()!=0) {
            throw new WorkAddException(MessageConstant.WORK_EXIT);
        }

        PlaceWorkUser placeWorkUser =new PlaceWorkUser();
        BeanUtils.copyProperties(placeWorkUserDTO,placeWorkUser);
        placeWorkUserMapper.insert(placeWorkUser);

    }


}
